package com.yjk1.example.myFirstSofawebBoot;

import com.yjk.example.Service;
import com.yjk1.example.myFirstSofawebBoot.framework.DynamicJarManageService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author yujiakui
 * @version 1.0
 * Email: jkyu@haiyi-info.com
 * date: 2018/11/12 18:26
 * description：
 **/
@RestController
public class MainController implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private DynamicJarManageService dynamicJarManageService;


    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(@RequestParam String info) {
        System.out.println("test----------->"+info);
        return "hello";
    }
    @RequestMapping(value = "/load",method = RequestMethod.GET)
    public String load(@RequestParam String path,@RequestParam String pluginName,@RequestParam String depPluginName){
        String info = "load---->path="+path+"------>pluginName="+pluginName;
        System.out.println(info);
        List<String> dependencyPluginNames = null;
        if (null != depPluginName && !"".equals(depPluginName)){
            dependencyPluginNames = Arrays.asList(depPluginName.split(","));
        }
        dynamicJarManageService.load(path,pluginName,dependencyPluginNames);
        return info;
    }

    @RequestMapping(value = "/regist",method = RequestMethod.GET)
    public String regist(@RequestParam String pluginName,@RequestParam String locationPattern) {
        String info = "regist----->pluginName="+pluginName + "--->locationPattern="+locationPattern;
        System.out.println(info);
        dynamicJarManageService.register(pluginName, locationPattern);
        return info;
    }

    @RequestMapping(value = "/unregist",method = RequestMethod.GET)
    public String unregist(@RequestParam String pluginName) {
        String info = "unregist---->pluginName="+pluginName;
        System.out.println(info);
        dynamicJarManageService.unRegister(pluginName);
        return info;
    }

    @RequestMapping(value = "/unload",method = RequestMethod.GET)
    public String unload(@RequestParam String pluginName) {
        String info = "unload---->pluginName="+pluginName;
        System.out.println(info);
        dynamicJarManageService.unload(pluginName);
        return info;
    }


    @GetMapping("/load1")
    public String load1() {
        System.out.println("load1-----");
        dynamicJarManageService.load("E:/code/BizPluginFramework/PluginBizTest/target/PluginBizTest-1.0-SNAPSHOT.jar"
                , "plugin", null);
        return "loadX";
    }

    @GetMapping("/load2")
    public String load2() {
        System.out.println("load2-----");
        dynamicJarManageService.load("E:/code/BizPluginFramework/daoDemo/target/daoDemo-1.0-SNAPSHOT.jar"
                , "daoPlugin", null);
        return "loadX";
    }

    @GetMapping("/regist2")
    public String regist2() {
        System.out.println("regist2-----");
        dynamicJarManageService.register("daoPlugin", "classpath*:com/yjk/**/*.**");
        return "registX";
    }

    @GetMapping("/regist1")
    public String regist1() {
        System.out.println("regist1-----");
        dynamicJarManageService.register("plugin", "classpath*:com/yjk/**/*.**");
        return "registX";
    }

    @GetMapping("/unregist1")
    public String unregist1() {
        System.out.println("unregist1-----");
        dynamicJarManageService.unRegister("plugin");
        return "unregistX";
    }

    @GetMapping("/unregist2")
    public String unregist2() {
        System.out.println("unregist2-----");
        dynamicJarManageService.unRegister("daoPlugin");
        return "unregistX";
    }

    @GetMapping("/unload1")
    public String unload1() {
        System.out.println("unload1-----");
        dynamicJarManageService.unload("plugin");
        return "unloadX";
    }

    @GetMapping("/unload2")
    public String unload2() {
        System.out.println("unload2-----");
        dynamicJarManageService.unload("daoPlugin");
        return "unloadX";
    }

    @GetMapping("/service")
    public String service() {
        StringBuilder stringBuilder = new StringBuilder("service->");
        System.out.println("service-----<br/>");
        Map<String, Service> resultMap = applicationContext.getBeansOfType(Service.class);
        for (Map.Entry<String, Service> ele : resultMap.entrySet()) {
            System.out.println("----name--" + ele.getKey());
            ele.getValue().say("yyyyyy");
            stringBuilder.append("----name--" + ele.getKey());
            stringBuilder.append("\n<br/>");
        }

        return stringBuilder.toString();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
